package com.uh.checking.proc;


import com.uh.checking.config.FileFilterCfg;
import com.uh.checking.config.SubJarShadeConfig;
import com.uh.checking.filter.IncludeMatchFilter;

import java.io.File;

import static com.uh.checking.proc.JarCheckingUtils.execute;
import static com.uh.checking.config.JarCheckingConstants.*;

public class SubJarShadeProcessor {

    private SubJarShadeConfig subConf;

    private File libsDir;

    private File mainClassesDir;

    public SubJarShadeProcessor(SubJarShadeConfig subConf, MainJarProcessor mainProcessor) {
        this.subConf = subConf;
        this.libsDir = new File(mainProcessor.getUnzipDir(), mainProcessor.getConfig().getPathLibs());
        this.mainClassesDir = new File(mainProcessor.getUnzipDir(), mainProcessor.getConfig().getPathClasses());
    }

    public void process() {
        FileFilterCfg filter = subConf.getIncludeFilter();
        String[] jarNames = libsDir.list(new IncludeMatchFilter(filter.getPerfix(), filter.getSubfix()));
        for (String jarName : jarNames)
        {
            File jarFile = new File(libsDir, jarName);
            processJar(jarFile);
        }
    }



    /**
     * 重构Jar包，会进行下面操作：
     */
    private void processJar(File jarFile) {
        System.out.println("\"Shade\" for Jar: " + jarFile + ", exists:" + jarFile.exists());

        String jarName = jarFile.getName();
        File jarDir = jarFile.getParentFile();
        File unzipDir = new File(jarDir, jarName.substring(0, jarName.length() - 4));//去除 .jar 后缀，作为解压目录

        for(String step : subConf.getSteps()) {
            switch (step) {
                case "clean":
                    //如果解压目录已经存在就删除后再重建
                    if(unzipDir.exists() == true) {
                        execute(CMD_RM + unzipDir.getPath());
                    }
                    unzipDir.mkdir();//新建jar包的解压目录
                    break;
                case "unzip":
                    System.out.println("unzipDir:" + unzipDir.getAbsolutePath());
                    execute("cd " + unzipDir.getPath() + " && jar -xvf ../" + jarName);//对jar进行解压
                    break;
                case "process":
                    //删除目录
                    if(subConf.getDeleteFiles() != null) {
                        for(String delFile : subConf.getDeleteFiles()) {
                            File mavenDir = new File(unzipDir, delFile);
                            if (mavenDir.exists()) {
                                execute(CMD_RM + mavenDir.getPath());
                            }
                        }
                    }
                    break;
                case "copy":
                    //递归拷贝各class文件
                    execute(CMD_CP + unzipDir.getPath() + "/* " + mainClassesDir.getPath() + "/");
                    break;
                case "cleanProcess":
                    //删除，过程中的解压目录
                    execute(CMD_RM + unzipDir.getPath());
                    //删除jar文件，因为jar中的文件已经合并到主程序classes目录和META-INF目录中
                    jarFile.delete();
                    break;
            }
        }

    }

}
